﻿using System;
using TIS.DAO;
using System.Data;

namespace TIS.Category
{
    public partial class Category : System.Web.UI.Page
    {
        NewsDAO _newsDao = new NewsDAO();
        CategoryDAO _categoryDao = new CategoryDAO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["lang"] == null)
                {
                    Session["lang"] = 1;
                }
                ListNews();
            }
        }
    
        public void ListNews()
        {
            string id = Request.QueryString["catId"];
            string page = Request.QueryString["page"];
            int langId = int.Parse(Session["lang"].ToString());
            int pageNumber = 1;
            if (id != null && page != null)
            {
                DataTable dtTable = _categoryDao.GetCategory(int.Parse(id), langId);
                // <img src="/images/baner-product.jpg" width="980" height="280" alt="Banner" />
                foreach (DataRow rows in dtTable.Rows)
                {
                    Session["CategoryId"] = id;
                    Session["CategorySEOUrl"] = rows["CategorySEOUrl"].ToString();
                    ltlMetaTitle.Text = rows["CategoryTitle"].ToString();
                    ltlMetaDescription.Text = rows["CategoryDescription"].ToString();
                    ltlMetaKeywords.Text = rows["CategoryKeyword"].ToString();
                    ltlTitleCategory.Text = rows["CategoryName"].ToString();
                    ltlImageCategory.Text = rows["CategoryImage"].ToString() == "" ? "<img src=\"/images/baner-product.jpg\" width=\"980\" height=\"280\" alt=\"Banner\" />" : "<img src=\""+rows["CategoryImage"].ToString()+"\" width=\"980\" height=\"280\" alt=\"Banner\" />";
                }
             
                    if (int.Parse(page) >= 2)
                    {
                        pageNumber = pageNumber + 1;
                    }
                

                DataTable dt = _newsDao.GetListNews(int.Parse(id), 5, pageNumber);
                DataTable dtRe = _newsDao.GetListNews(int.Parse(id), 5, pageNumber + 1);
                rpList.DataSource = dt;
                rpList.DataBind();
       
                rpTinTrienKhai.DataSource = dtRe;
                rpTinTrienKhai.DataBind();

                if (dtRe.Rows.Count > 0)
                {
                    
                }
                else
                {
                    lbTinTrienKhai.Visible = false;
                }

                if (pageNumber == 1)
                {

                    ltlPrev.Text = "<a href=\"" + LinkUtil.linkCageory(id.ToString(), page.ToString(), Session["CategorySEOUrl"].ToString()) + "\" class=\"prev-page\" > </a>";
                }
                else
                {

                    ltlPrev.Text = "<a href=\"" + LinkUtil.linkCageory(id.ToString(), (int.Parse(page) - 1).ToString(), Session["CategorySEOUrl"].ToString()) + "\" class=\"prev-page\" > </a>"; ;
                }

                if (dtRe.Rows.Count > 0)
                {
                    ltlNext.Text = "<a href=\"" + LinkUtil.linkCageory(id.ToString(), (int.Parse(page) + 1).ToString(), Session["CategorySEOUrl"].ToString()) + "\" class=\"next-page\" > </a>"; ;

                }
                else
                {
                    ltlNext.Text = "<a href=\"#\"  class=\"next-page\" ></a>";

                }
               
            }
        }

    }
}